package org.linloong.modules.apis.monitor.sync;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.linloong.modules.apis.monitor.entity.WeatherData;
import org.linloong.modules.apis.monitor.entity.WeatherDataParsed;
import org.linloong.modules.apis.monitor.mapper.WeatherDataMapper;
import org.linloong.modules.apis.monitor.mapper.WeatherDataParsedMapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * @author dbo
 */
@Service
@RequiredArgsConstructor
public class HistoryClearTask {

    private final WeatherDataMapper weatherDataMapper;
    private final WeatherDataParsedMapper  weatherDataParsedMapper;

    // 每天凌晨0点执行，删除1个月前的数据
    @Scheduled(cron = "0 0 0 * * ?")
    public void clearHistory() {
        LocalDateTime lastMonth = LocalDateTime.now().minusMonths(1);
        Long timestamp = lastMonth.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        weatherDataMapper.delete(new LambdaQueryWrapper<WeatherData>().le(WeatherData::getTimestamp, timestamp));
        weatherDataParsedMapper.delete(new LambdaQueryWrapper<WeatherDataParsed>().le(WeatherDataParsed::getTimestamp, timestamp));
    }
}
